package com.haierCamera.customapplication.ui.user.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.UserRepo;
import com.haierCamera.customapplication.api.vo.GetUserAddress;
import com.haierCamera.customapplication.databinding.HzklActivityAddressBinding;
import com.haierCamera.customapplication.databinding.HzklActivityRepairBinding;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import javax.inject.Inject;

import dagger.Lazy;

/**
 * Created by AdministratorZJ
 * on 2020/3/24.
 */
public class HZKLUserAddressActivity extends BaseActivity {
    private HzklActivityAddressBinding binding;
    @Inject
    UserRepo repo;
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_address);
        setSupportActionBar(binding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initData();
    }

    private void initData() {
        getAddress();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.hzkl_yes, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.hzkl_yes:
                saveRepairUser();
                break;
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void saveRepairUser() {
        String prople = binding.repairPeople.getText().toString();
        String phone = binding.repairPhone.getText().toString();
        String address = binding.repairAddress.getText().toString();
        if (TextUtils.isEmpty(prople)) {
            Toast.makeText(HZKLUserAddressActivity.this, "请输入联系人", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(HZKLUserAddressActivity.this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(address)) {
            Toast.makeText(HZKLUserAddressActivity.this, "请输入地址", Toast.LENGTH_SHORT).show();
            return;
        }
        setRepairUser(prople, phone, address);
    }

    private void setRepairUser(String prople, String phone, String address) {
        repo.setUserAddress(address, prople, phone).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    dismissLoadingDialog();
                    Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                default:
                    break;
            }
        });
    }

    private void getAddress() {
        repo.getUserAddress().observe(this, getUserAddressResource -> {
            switch (getUserAddressResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(this, getUserAddressResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(getUserAddressResource);
                    break;
                case SUCCESS:
                    dismissLoadingDialog();
                    GetUserAddress data = getUserAddressResource.data;
                    binding.setSetAddress(data);
                    break;
                default:
                    break;
            }
        });
    }
}
